package framework.graphics;

import javax.microedition.khronos.opengles.GL10;

public class ScaledAnimation extends Animation {

	private float fromScaleX;
	private float toScaleX;
	private float scalePerSecondX;
	private float fromScaleY;
	private float toScaleY;
	private float scalePerSecondY;
	
	private float currentScaleX = 1;
	private float currentScaleY = 1;
	
	public ScaledAnimation(float delay, Texture[] frames, 
			float fromScaleX, float toScaleX, float scalePerSecondX,
			float fromScaleY, float toScaleY, float scalePerSecondY) {
		super(delay, frames);
		this.fromScaleX = fromScaleX;
		this.toScaleX = toScaleX;
		this.scalePerSecondX = scalePerSecondX;
		this.fromScaleY = fromScaleY;
		this.toScaleY = toScaleY;
		this.scalePerSecondY = scalePerSecondY;
		this.currentScaleX = fromScaleX;
		this.currentScaleY = fromScaleY;
	}
	
	private float kx = 1;
	private float ky = 1;

	@Override
	public void draw(float deltaTime, GL10 gl, float x, float y) {
		updateFrames(deltaTime);
		currentScaleX += kx * scalePerSecondX * deltaTime;
		if ( ! (fromScaleX <= currentScaleX && currentScaleX <= toScaleX) ) {
			if (currentScaleX < fromScaleX) {
				currentScaleX = fromScaleX;
				kx = 1;
			}
			if (currentScaleX > toScaleX) {
				currentScaleX = toScaleX;
				kx = -1;
			}
		}
		currentScaleY += ky * scalePerSecondY * deltaTime;
		if ( ! (fromScaleY <= currentScaleY && currentScaleY <= toScaleY) ) {
			if (currentScaleY < fromScaleY) {
				currentScaleY = fromScaleY;
				ky = 1;
			}
			if (currentScaleY > toScaleY) {
				currentScaleY = toScaleY;
				ky = -1;
			}
		}
		frames[currentFrame].draw(gl, x, y, 0, 0, 0, currentScaleX, currentScaleY);
	}

}
